package cn.dankal.flyaudio.personal;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.kongzue.dialog.v2.SelectDialog;

import java.util.List;

import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.common.bus.LiveDataBusConstants;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.AddressManagerAdapter;
import cn.dankal.flyaudio.contract.AddressManagerContract;
import cn.dankal.basiclib.pojo.AddressEntity;
import cn.dankal.flyaudio.presenter.AddressManagerPresenter;

public class AddressManagerActivity extends BaseActivity implements AddressManagerContract.View {

    private RecyclerView recyclerView;

    private AddressManagerAdapter adapter;

    private AddressManagerPresenter mAddressManagerPresenter;

    private boolean isPick = false;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_manager;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("地址管理");
        isPick = getIntent().getBooleanExtra("is_pick", false);
        mAddressManagerPresenter = new AddressManagerPresenter();
        mAddressManagerPresenter.attachView(this);
        recyclerView = findViewById(R.id.recyclerview);
        adapter = new AddressManagerAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        LiveEventBus.get(LiveDataBusConstants.ADD_ADDRESS_SUCCESS, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                mAddressManagerPresenter.getMyAddress();
            }
        });
        adapter.setListener(new AddressManagerAdapter.HandleListener() {
            @Override
            public void setDefault(int pos) {
                SelectDialog.show(AddressManagerActivity.this, "提示", "是否设置该地址为默认地址", "确定",
                        (dialog, which) -> {
                    AddressEntity entity = adapter.getDatas().get(pos);
                    entity.setDefaultAddress(true);
                    mAddressManagerPresenter.UpdateAddress(entity,true);
                }, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
            }

            @Override
            public void delete(int pos) {
                SelectDialog.show(AddressManagerActivity.this, "提示", "是否删除该地址", "确定", (dialog, which) -> {
                    AddressEntity entity = adapter.getDatas().get(pos);
                    mAddressManagerPresenter.deleteAddress(entity.getUuid());
                }, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
            }

            @Override
            public void onItemClick(AddressEntity data) {
                if (isPick) {
                    Intent intent = new Intent();
                    intent.putExtra("address", data);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        findViewById(R.id.tv_create_address).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(AddressManagerActivity.this, AddAddressActivity.class));
            }
        });
        initLiveDataBus();
        mAddressManagerPresenter.getMyAddress();
    }

    private void initLiveDataBus() {
        LiveEventBus.get(LiveDataBusConstants.ADD_ADDRESS_SUCCESS, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                mAddressManagerPresenter.getMyAddress();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAddressManagerPresenter.detachView();
    }

    @Override
    public void handleAddressList(List<AddressEntity> addressEntities) {
        if (addressEntities.isEmpty()) {
            adapter.clearData();
            return;
        }
        adapter.addDatas(addressEntities, true);
    }

    @Override
    public void updateSuccess() {
        mAddressManagerPresenter.getMyAddress();
    }
}
